這一篇將深入探討如何在 K3s 叢集中整合進階元件,以提升其功能性、網路效能、負載平衡能力以及持久化儲存的彈性。K3s 雖然輕量,但在生產環境中,往往需要更強大的網路、負載平衡和儲存解決方案。本篇將介紹如何導入以下關鍵元件,將 K3s 叢集從基礎配置升級為功能更完善、效能更優異的進階部署:
Cilium 是一個基於 eBPF (extended Berkeley Packet Filter) 技術的開源軟體,專為 Kubernetes 叢集提供高性能的網路、安全和可觀察性。相較於傳統的 CNI 解決方案,Cilium 能夠在 Linux 核心中直接處理網路封包,從而實現更低的延遲和更高的吞吐量。
Cilium Without kube-proxy 的優勢:
傳統上,Kubernetes 使用 kube-proxy
來處理服務的負載平衡和網路轉發。然而,Cilium 能夠利用 eBPF 直接在 Linux 核心中處理這些功能,從而繞過 kube-proxy
。這樣做的好處包括:
kube-proxy
進程,減少了叢集節點上的資源佔用。kube-proxy
可以簡化 Kubernetes 的網路堆疊,減少潛在的配置複雜性和故障點。HAProxy Ingress 是一個基於高性能 HAProxy 的 Kubernetes Ingress Controller。Ingress Controller 負責將外部流量路由到叢集內部的服務,是將應用程式暴露給外部世界的關鍵組件。HAProxy 以其高性能、高可靠性和靈活性而聞名,作為 Ingress Controller,它能夠提供:
相較於 K3s 預設的 Traefik,HAProxy Ingress 在某些場景下可能提供更精細的控制和更強大的企業級功能,尤其是在需要高度定制化負載平衡策略時。
Longhorn 是一個輕量級、可靠且易於使用的 Kubernetes 分佈式區塊儲存系統。它由 Rancher Labs 開發,並作為 CNCF (Cloud Native Computing Foundation) 沙箱項目。Longhorn 通過 CSI 接口為 Kubernetes 提供持久化儲存,允許有狀態應用程式在叢集中動態地請求和使用儲存空間。
Longhorn 的主要特點:
相關連結:
本節將引導您完成 K3s 叢集與上述進階元件的安裝過程。
並且 mirror registry 方式安裝來大幅節省 pull image bandwidth
在開始安裝之前,請確保您已完成以下準備工作:
curl
,wget
和 sudo
。這功能將每個 k3s node 成為 mirror registry
藉此節省 pull image 所需的 bandwidth
in each node config
sudo mkdir -p /etc/rancher/k3s/
sudo tee /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
"*":
EOF
Manually download k3s
cd /tmp/
wget https://github.com/k3s-io/k3s/releases/download/v1.32.5%2Bk3s1/k3s
chmod +x k3s
# copy to node (repeat node1~4)
scp k3s 192.168.56.101:/tmp
Manually download cilium cli
CILIUM_CLI_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/cilium-cli/main/stable.txt)
CLI_ARCH=`dpkg --print-architecture`
if [ "$(uname -m)" = "aarch64" ]; then CLI_ARCH=arm64; fi
curl -L --fail --remote-name-all https://github.com/cilium/cilium-cli/releases/download/${CILIUM_CLI_VERSION}/cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}
sha256sum --check cilium-linux-${CLI_ARCH}.tar.gz.sha256sum
sudo tar xzvfC cilium-linux-${CLI_ARCH}.tar.gz /tmp
rm cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}
scp cilium 192.168.56.101:/tmp
# 在第一個伺服器節點上執行 (e.g., node1)
sudo cp /tmp/k3s /usr/local/bin/k3s
sudo cp /tmp/cilium /usr/local/bin/cilium
export INSTALL_K3S_VERSION=v1.32.5+k3s1
export INSTALL_K3S_EXEC="server \
--disable=traefik \
--write-kubeconfig-mode 644 \
--cluster-init \
--tls-san=192.168.56.100 \
--flannel-backend=none \
--disable-network-policy \
--embedded-registry \
--disable-kube-proxy"
curl -sfL https://get.k3s.io | sh -s -
# get token (use later)
sudo cat /var/lib/rancher/k3s/server/node-token
mkdir $HOME/.kube
sudo cp /etc/rancher/k3s/k3s.yaml $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
tee -a ~/.bashrc <<EOF
source <(k3s kubectl completion bash)
alias kubectl="k3s kubectl"
alias k="k3s kubectl"
complete -o default -F __start_kubectl k
EOF
source ~/.bashrc
這時 k get node
會看到 STATUS: NotReady
這是因為我們還沒安裝 CNI 導致, 為正常現象
$ k describe node node1
Conditions:
Type Status LastHeartbeatTime LastTransitionTime Reason Message
---- ------ ----------------- ------------------ ------ -------
EtcdIsVoter True Sat, 14 Jun 2025 01:35:17 +0000 Sat, 14 Jun 2025 01:25:17 +0000 MemberNotLearner Node is a voting member of the etcd cluster
MemoryPressure False Sat, 14 Jun 2025 01:35:32 +0000 Sat, 14 Jun 2025 01:25:09 +0000 KubeletHasSufficientMemory kubelet has sufficient memory available
DiskPressure False Sat, 14 Jun 2025 01:35:32 +0000 Sat, 14 Jun 2025 01:25:09 +0000 KubeletHasNoDiskPressure kubelet has no disk pressure
PIDPressure False Sat, 14 Jun 2025 01:35:32 +0000 Sat, 14 Jun 2025 01:25:09 +0000 KubeletHasSufficientPID kubelet has sufficient PID available
Ready False Sat, 14 Jun 2025 01:35:32 +0000 Sat, 14 Jun 2025 01:25:09 +0000 KubeletNotReady container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:Network plugin returns error: cni plugin not initialized
# install CNI
cilium install \
--version v1.17.4 \
--set=ipam.operator.clusterPoolIPv4PodCIDRList="10.42.0.0/16" \
--set kubeProxyReplacement=true \
--set k8sServiceHost=192.168.56.100 \
--set k8sServicePort=6443
# Validate
k -n kube-system get cm cilium-config -o yaml | grep kube-proxy-replacement
k -n kube-system exec ds/cilium -- cilium-dbg status | grep KubeProxyReplacement
cilium status --wait
# run test
cilium connectivity test
# cleanup
kubectl delete ns cilium-test
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
helm repo add longhorn https://charts.longhorn.io
helm repo update
helm install longhorn longhorn/longhorn --namespace longhorn-system --create-namespace --version 1.9.0
helm repo add haproxytech https://haproxytech.github.io/helm-charts
helm repo update
helm install haproxy-kubernetes-ingress haproxytech/kubernetes-ingress \
--create-namespace \
--namespace haproxy-controller
# 在第二個和第三個伺服器節點上執行 (e.g., node2, node3)
sudo cp /tmp/k3s /usr/local/bin/k3s
sudo cp /tmp/cilium /usr/local/bin/cilium
export INSTALL_K3S_VERSION=v1.32.5+k3s1
export INSTALL_K3S_EXEC="server \
--disable=traefik \
--write-kubeconfig-mode 644 \
--server https://192.168.56.100:6443 \
--tls-san=192.168.56.100 \
--flannel-backend=none \
--disable-network-policy \
--embedded-registry \
--disable-kube-proxy"
export K3S_TOKEN=<從第一個伺服器節點獲取的令牌>
curl -sfL https://get.k3s.io | sh -s -
sudo cp /tmp/k3s /usr/local/bin/k3s
export INSTALL_K3S_VERSION=v1.32.5+k3s1
export INSTALL_K3S_EXEC="agent \
--server https://192.168.56.100:6443"
export K3S_TOKEN=<從第一個伺服器節點獲取的令牌>
curl -sfL https://get.k3s.io | sh -s -
以上 一個基本功能完整的 k3s cluster 就建起來了
對比安裝 k8s cluster 可是簡單不少
本文同步發表至 k3s install advance 2